/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-18 Marco Riccardi - initial implementation
 *
 */
package org.mizartools.dli;

import java.util.LinkedList;

public class Pred extends Formula {

    private ItemId itemId = null;
    private LinkedList<Term> termList = new LinkedList<Term>();

	private Pred(){
		super();
	}
	
	public Pred(ItemId itemId, LinkedList<Term> termList) throws DliException {
		if (itemId == null || termList == null){
			throw new DliException();
		}
		this.itemId = itemId;
		this.termList = termList;
    }
    
	public static Pred newInstance(String string) throws DliParseException {
		Pred pred = new Pred();
		int position = string.indexOf("(");
		if (position == -1) {
			pred.itemId = ItemId.newInstance(string);
			return pred;
		} else {
			pred.itemId = ItemId.newInstance(string.substring(0, position));
		}
		string = string.substring(position+1, string.length()-1);
		position = Utility.searchSeparationComma(string);
		while (position != -1) {
			pred.termList.add(Term.newInstance(string.substring(0, position)));
			string = string.substring(position+1, string.length());
			position = Utility.searchSeparationComma(string);
		}
		pred.termList.add(Term.newInstance(string));
		return pred;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(itemId.toString());
		if (termList.size()>0){
			sb.append("(");
			boolean first = true;
			for (Term term : termList){
				if (first) first = false;
				else sb.append(",");
				sb.append(term.toString());
			}
			sb.append(")");
		}
		return sb.toString();
	}

	public ItemId getItemId() {
		return itemId;
	}

	public LinkedList<Term> getTermList() {
		return termList;
	}
	
}
